package com.buaa.hyf.testviewpager;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private List<View> allViews;
    private MyPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager = (ViewPager) findViewById(R.id.pager);
        allViews = new ArrayList<>();

        //获取view，Inflate()作用就是将xml定义的一个布局找出来
        View v1 = LayoutInflater.from(this).inflate(R.layout.test_01,null);
        View v2 = LayoutInflater.from(this).inflate(R.layout.test_02,null);
        View v3 = LayoutInflater.from(this).inflate(R.layout.test_03,null);

        allViews.add(v1);
        allViews.add(v2);
        allViews.add(v3);

        pager.setOffscreenPageLimit(3);//安卓自带bug，需要指明页面的个数
        adapter = new MyPagerAdapter(allViews);
        pager.setAdapter(adapter);
    }
}
